package com.song.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.song.admin.mapper.AdminUserMapper;
import com.song.admin.service.AdminUserService;
import com.song.api.BaseService;
import com.song.exception.GraceException;
import com.song.grace.result.ResponseStatusEnum;
import com.song.pojo.AdminUser;
import com.song.pojo.bo.NewAdminBO;
import com.song.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AdminUserServiceImpl extends BaseService implements AdminUserService {

    @Autowired
    public AdminUserMapper adminUserMapper;

    @Autowired
    public Sid sid;

    private static final Logger LOG = LoggerFactory.getLogger(AdminUserServiceImpl.class);

    /**
     * 获得管理员用户信息
     */
    @Override
    public AdminUser queryAdminByUsername(String username) {
        Example adminExample = new Example(AdminUser.class);
        Example.Criteria criteria = adminExample.createCriteria();
        criteria.andEqualTo("username", username);
        AdminUser admin = adminUserMapper.selectOneByExample(adminExample);
        return admin;
    }

    /**
     * 新增管理员
     *
     * @param newAdminBO
     */
    @Override
    public void CreateAdminUser(NewAdminBO newAdminBO) {

        String adminId = sid.nextShort();

        AdminUser adminUser = new AdminUser();
        adminUser.setId(adminId);
        adminUser.setUsername(newAdminBO.getUsername());
        adminUser.setAdminName(newAdminBO.getAdminName());


        // 如果密码不为空，则使用bcrypt加密，比md5安全需更好
        if (StringUtils.isNotBlank(newAdminBO.getPassword())) {
            String bPwd = BCrypt.hashpw(newAdminBO.getPassword(), BCrypt.gensalt());
            adminUser.setPassword(bPwd);
        }

        if (StringUtils.isNotBlank(newAdminBO.getFaceId())) {
            adminUser.setFaceId(newAdminBO.getFaceId());
        }

        adminUser.setCreatedTime(new Date());
        adminUser.setUpdatedTime(new Date());

        int result = adminUserMapper.insert(adminUser);
        if (result != 1) {
            GraceException.display(ResponseStatusEnum.ADMIN_CREATE_ERROR);
        }

    }

    /**
     * 分页查询管理员列表
     *  @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedGridResult queryAdminList(Integer page, Integer pageSize) {
        Example adminExample = new Example(AdminUser.class);
        adminExample.orderBy("createdTime").desc();
        PageHelper.startPage(page,pageSize);
        List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminExample);
        return setPagedGridResult(adminUserList, page);
    }

//    private PagedGridResult setPagedGridResult(List<?> adminUserList,Integer page){
//        PageInfo<?> pageInfo = new PageInfo<>(adminUserList);
//        PagedGridResult pagedGridResult = new PagedGridResult();
//        pagedGridResult.setPage(page);
//        pagedGridResult.setRows(adminUserList);
//        pagedGridResult.setRecords(pageInfo.getPages());
//        pagedGridResult.setTotal(pageInfo.getTotal());
//        return pagedGridResult;
//    }
}
